﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Diagnostics.Contracts;
using QuantumWorkspace.SessionManager;
using QuantumWorkspaces.DomainEntities;


namespace OpenLS.Spreadsheet.UI
{
    class WorksheetHelper
    {
        /// <summary>
        /// accessor return true if the operation is allowed
        /// </summary>
        /// <param name="accessor">returns false if the operation is _not_ allowed</param>
        /// <returns></returns>
        static internal Func<Worksheet, bool> ProtectionFunction(Func<SheetProtection, bool> accessor)
        {
            return ws => HasNonProtectedSelectionImp(ws, accessor);
        }
        /// <summary>
        /// accessor return true if the operation is allowed
        /// </summary>
        /// <param name="accessor">returns false if the operation is _not_ allowed</param>
        /// <returns></returns>
        static internal Func<Worksheet, bool> IsAllowed(Func<SheetProtection, bool> accessor)
        {
            return ws => HasNonProtectedSelectionImp(ws, accessor);
        }
        public static Func<Worksheet, bool> DefaultProtectionFunction
        {
            get
            {
                return ProtectionFunction(p => p.Sheet);
            }
        }
        public static Func<Worksheet, bool> FormatCellProtectionFunction
        {
            get
            {
                return ProtectionFunction(p => (p.FormatCells));
            }
        }

        public static Func<Worksheet, bool> IsLoggedAndFormatCellProtectionFunction
        {
            get
            {
                return ProtectionFunction(p => (QuantumUserSession.IsLogged & p.FormatCells));
            }
        }

        /// <summary>
        /// return true if the operation is allowed
        /// </summary>
        /// <param name="worksheet"></param>
        /// <param name="isProtectedAccessor"></param>
        /// <returns></returns>
        private static bool HasNonProtectedSelectionImp(Worksheet worksheet, Func<SheetProtection, bool> isProtectedAccessor)
        {
            if (worksheet == null)
                return false;
            if (worksheet.Selection == null)
                return false;
            if (worksheet.Protection == null)
                return true;
            if (!worksheet.Protection.Sheet)
                return true;
            return !isProtectedAccessor(worksheet.Protection);
        }
        internal static void OnAlways(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = true;
            args.Handled = true;
        }
        internal static Worksheet GetWorksheet(object sender)
        {
            Contract.Requires(sender != null);
            return SpreadsheetElement.GetWorksheet(sender);
        }
        internal static WorksheetViewModel GetWorksheetViewModel(object sender)
        {
            Contract.Requires(sender != null);
            Contract.Ensures(Contract.Result<WorksheetViewModel>() != null);
            return SpreadsheetElement.GetWorksheetViewModel((DependencyObject) sender);
        }

        internal static void OnHasActiveCell(object sender, CanExecuteRoutedEventArgs args)
        {
            Worksheet worksheet = GetWorksheet(sender);
            if (worksheet == null)
                return;
            args.CanExecute = worksheet.ActiveCell != null;
            args.Handled = true;
        }

        internal static void OnHasSelection(object sender, CanExecuteRoutedEventArgs args)
        {
            args.Handled = true;
            Worksheet worksheet = GetWorksheet(sender);
            if (worksheet == null)
                return;
            args.CanExecute = worksheet.Selection != null;
        }
        internal static bool OnHasSelection2(Worksheet worksheet)
        {
            if (worksheet == null)
                return false;
            return worksheet.Selection != null;
        }

        private static bool OnHasNonProtectedSelection(Worksheet worksheet)
        {
            if (worksheet == null)
                return false;
            return worksheet.Selection != null && worksheet.Protection == null;
        }
        

    }
}